package com.itheima.mall.web.sms;


import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.sms.HomeAdvertiseService;
import com.itheima.mall.entity.domain.sms.HomeAdvertise;
import com.itheima.mall.entity.domain.vo.smsre.ResultAll;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;

@ResponseBody
@RestController
@RequestMapping("/home/advertise")
@CrossOrigin(origins = "*")
public class HomeAdvertiseController {


    @DubboReference
    private HomeAdvertiseService homeAdvertiseService;

    @PostMapping("/create")
    public Result create(@RequestBody HomeAdvertise homeAdvertise) {
        System.out.println("1111111111111111");
        System.out.println(homeAdvertise);
        homeAdvertiseService.save(homeAdvertise);
        System.out.println("1111111111111111");
        return Result.success("成功");
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println("22222222222");
        for (Long id : ids) {
            homeAdvertiseService.removeById(id);
        }
        System.out.println("22222222222");
        return Result.success("成功");
    }

    @GetMapping("/list")
    public Result list(
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize

    ) {
        System.out.println("000000" + endTime);
        System.out.println("000000" + name);
        System.out.println("000000" + type);
        System.out.println("000000" + pageNum);
        System.out.println("000000" + pageSize);
        ResultAll resultAll = homeAdvertiseService.page(endTime, name, pageNum, pageSize, type);

        return Result.success(resultAll);
    }

    @RequestMapping("/update/status/{id}")
    public Result updateSort(@PathVariable Long id, Integer status) {
        System.out.println(id);
        System.out.println(status);
        homeAdvertiseService.updateSort(id, status);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getid(@PathVariable Integer id) {
        System.out.println(id);
        HomeAdvertise homeAdvertise = homeAdvertiseService.getById(id);
        System.out.println(homeAdvertise);
        return Result.success(homeAdvertise);
    }

    @PostMapping("/update/{id}")
    public Result update(@PathVariable Long id, @RequestBody HomeAdvertise homeAdvertise) {
        homeAdvertise.setId(id);
        homeAdvertiseService.updateById(homeAdvertise);
        return Result.success("成功");
    }

}
